<?php
/**
 * CreateTime: 2023/11/21 10:57
 * User:LinQ
 */

namespace App\State\User;

use App\Models\Users\UsersModel;
use App\State\StateInterface;

class UserEnableState implements StateInterface
{
    // 启用
    const USER_ENABLE_STATUS_ENABLE = 1;
    // 禁用
    const USER_ENABLE_STATUS_DISABLE = 2;

    public function getStateDescription($status): string
    {
        return match ($status){
            self::USER_ENABLE_STATUS_ENABLE => trans('key.enable'),
            self::USER_ENABLE_STATUS_DISABLE => trans('key.disable'),
            default => "",
        };
    }

    public function stateChange(int $user_id, int $status, int $toStatus): bool
    {
        $statusStr = $this->getStateDescription($status);
        if(!$statusStr){
            s_fail_return(null,trans('message.start_status_err',[
                'name' => trans('key.user')
            ]));
        }
        $toStatusStr = $this->getStateDescription($toStatus);
        if(!$toStatusStr){
            s_fail_return(null,trans('message.change_status_err',[
                'name' => trans('key.user')
            ]));
        }
        try {
            (new UsersModel())->where('user_id',$user_id)
                ->where('enable_status',$status)
                ->update([
                    'enable_status' => $toStatus
                ]);
            return true;
        }catch (\Exception $exception){
            s_fail_return(null,trans('message.update_status_err'));
            return false;
        }
    }
}